/*
 * @Descripttion:
 * @version:
 * @Author: ChenXiang
 * @Date: 2021-05-19 19:44:31
 * @LastEditors: ChenXiang
 * @LastEditTime: 2021-06-03 17:03:37
 */
import { createSlice } from "@reduxjs/toolkit";
import http from "@/common/request";
export const incrementAsync = (amount) => (dispatch) => {
  http.Demo.tables().then((res) => {
    dispatch(incrementByAmount(res.length));
  });
};

export const selectCount = (state) => state.value;

export const counterSlice = createSlice({
  name: "counter",
  initialState: {
    value: 0,
  },
  reducers: {
    increment: (state) => {
      // 这里是因为使用了Immer库，所以能够使用这种直接修改state的语法，但其实并不是mutate
      state.value += 1;
    },
    decrement: (state) => {
      state.value -= 1;
    },
    incrementByAmount: (state, action) => {
      state.value += action.payload;
    },
    getAll: (state, action) => {
      state.value += action.payload;
    },
  },
});

export const { increment, decrement, incrementByAmount, getAll } =
  counterSlice.actions;

export default counterSlice.reducer;
